package game.engine;

import game.main.GameFrame;
import game.menu.MenuContainer;
import game.menu.MenuElement;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.event.KeyEvent;

public abstract class AbstractMenuEngine extends AbstractEngine {

	public static final int CONTAINER_XPOS = 50;
	public static final int CONTAINER_YPOS = 80;

	public MenuContainer container;

	protected AbstractMenuEngine() {
		initialize();
	}

	protected void actionForDown(MenuElement selectedElement) {
		container.cursorDown();
	}

	protected abstract void actionForEnter(MenuElement selectedElement);

	protected void actionForUp(MenuElement selectedElement) {
		container.cursorUp();
	}

	protected abstract MenuContainer createContainer();

	private void fillBackGround(Graphics2D g2d) {
		Paint oldPaint = g2d.getPaint();
		g2d.setPaint(Color.BLACK);
		g2d.fillRect(0, 0, GameFrame.singleton().getWidth(), GameFrame.singleton().getHeight());
		g2d.setPaint(oldPaint);
	}

	@Override
	public void initialize() {
		this.container = createContainer();
	}

	@Override
	public boolean isReturnAble() {
		return false;
	}

	@Override
	public void keyPressed(KeyEvent e) {
		navigationActions(e);
		MenuElement selectedElement = container.getSelectedElement();
		if (KeyEvent.VK_UP == e.getKeyCode()) {
			actionForUp(selectedElement);
		}
		if (KeyEvent.VK_DOWN == e.getKeyCode()) {
			actionForDown(selectedElement);
		}
		if (KeyEvent.VK_ENTER == e.getKeyCode()) {
			actionForEnter(selectedElement);
		}

	}

	@Override
	public void keyReleased(KeyEvent e) {
		// NOP
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// NOP
	}

	protected void navigationActions(KeyEvent e) {
		return;
	}

	@Override
	public void render(Graphics2D g2d) {
		g2d.clearRect(0, 0, GameFrame.singleton().getWidth(), GameFrame.singleton().getHeight());
		fillBackGround(g2d);
		container.drawMenu(g2d);
	}

	@Override
	public void update() {
		// NOP
	}

}